﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mock1G3.BaseEntities;
using Mock1G3.BussinessLogic;
using Mock1G3.DataAccess;

namespace Mock1G3.UserControl.WithDraw
{
    public partial class EnterAmount : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnAccept_Click(object sender, EventArgs e)
        {
            WithDraw();
        }
        protected void btnBack_Click(object sebder, EventArgs e)
        {
            foreach (Control item in this.Parent.Controls)
            {
                if (item.ID.Equals("ucSelectAmount"))
                {
                    item.Visible = true;
                    continue;
                }
                item.Visible = false;
            }
        }
        public void WithDraw()
        {
            Card card = new Card();
            ATM atm = new ATM();
            card.AccountID = Int32.Parse(Session["AccountID"].ToString());
            card.CardNo = Session["CardNo"].ToString();
            atm.ATMID = Int32.Parse(Session["ATMID"].ToString());
            try
            {
                decimal amount = decimal.Parse(txtEnterAmount.Text);
                if (!WithDrawAccess.CheckValidAmountWithMaxValue(amount))
                {
                    WebMsgBox.Show("You can't with draw more than 15000000. Please re-enter amount greater!");
                    txtEnterAmount.Text = string.Empty;
                }
                else if (!WithDrawAccess.CheckValidAmountWithBalance(amount, card.AccountID))
                {
                    WebMsgBox.Show("You don't have enought balance. Please select agian!");
                    txtEnterAmount.Text = string.Empty;
                }
                else if (!WithDrawAccess.CheckOverDrawLimit(amount, card.AccountID))
                {
                    WebMsgBox.Show("Your withdraw was limited. You can't withdraw today.");
                    txtEnterAmount.Text = string.Empty;
                }
                else if (!WithDrawAccess.CheckValidAmountWithMinValue(amount))
                {
                    WebMsgBox.Show("Your amount is shorter than 50000. Please re-enter amount greater!");
                    txtEnterAmount.Text = string.Empty;
                }
                else if (!WithDrawAccess.CheckAmountDivide(amount))
                {
                    WebMsgBox.Show("Invalid amount. Your amount must divide to 50000. Please re-enter!");
                    txtEnterAmount.Text = string.Empty;
                }
                else
                {
                    WithDrawLogic.WithDrawMoney(amount, card.AccountID, atm.ATMID, card.CardNo);
                    WebMsgBox.Show("Withdraw successful!");
                    foreach (Control item in this.Parent.Controls)
                    {
                        if (item.ID.Equals("ucPrintReceipt"))
                        {
                            item.Visible = true;
                            continue;
                        }
                        item.Visible = false;
                    }
                }
            }
            catch (Exception e)
            {
                WebMsgBox.Show(e.Message);
            }
            
        }
    }
}